package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @description:
 * @author: Alang
 * @date: 2020/6/29-17:50
 * @Version: 1.0.0
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 新增
     * @author Alang
     * @date 2020/6/29-18:25
     * @param permission
     * @return void
     */
    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    /**
     * 分页查询
     * @author Alang
     * @date 2020/6/29-18:26
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return com.itheima.entity.PageResult
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        List<Permission> permissionList = permissionMapper.findPageByCondition(queryString);
        PageHelper.startPage(currentPage,pageSize);
        PageInfo<Permission> pageInfo = new PageInfo<>(permissionList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 修改
     * @author Alang
     * @date 2020/6/29-21:08
     * @param permission
     * @return void
     */
    @Override
    public void update(Permission permission) {
        permissionMapper.update(permission);
    }

    @Override
    public void deleteById(Integer id) {
        //根据id查询该权限有没有被中间表引用
        Integer count = permissionMapper.findCountByPId(id);
        if (count>0){
            throw new RuntimeException("该权限已被角色引用，不能删除！");
        }else {
            permissionMapper.deleteById(id);
        }
    }
}
